package cn.com.api.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiJuan;
import cn.com.api.entity.ApiType;
import cn.com.api.mapper.ApiGoodsMapper;
import cn.com.api.mapper.ApiJuanMapper;
import cn.com.api.mapper.ApiJuanTJMapper;
import cn.com.api.mapper.ApiTypeMapper;
import cn.com.api.service.ApiJuanService;
import cn.com.util.QRCodeUtils;
import cn.com.util.*;
import cn.com.util.*;

@Service
public class ApiJuanServiceImpl implements ApiJuanService {

	@Autowired
	private ApiJuanMapper apiJuanMapper;
	@Autowired
	private ApiJuanTJMapper apiJuanTJMapper;
	@Autowired
	private ApiGoodsMapper apiGoodsMapper;
	@Autowired
	private ApiTypeMapper apiTypeMapper;

	@Override
	public boolean insert(ApiJuan apiJuan) {
		return apiJuanMapper.insert(apiJuan);
	}

	@Override
	public List<Map<String, String>> select() {
		return apiJuanMapper.select();
	}

	@Override
	public List<Map<String, String>> selectByModListPage(Map<String, Object> map, PageInfo pageInfo) {
		return apiJuanMapper.selectByModListPage(map, pageInfo);
	}

	@Override
	public List<Map<String, String>> selectByUid(String uid) {
		return apiJuanMapper.selectByUid(uid);
	}

	@Override
	public Map<String, Object> selectById(String id) {
		return apiJuanMapper.selectById(id);
	}

	@Override
	public List<Map<String, String>> selectByArea(String cityid, String gtype) {
		return apiJuanMapper.selectByArea(cityid, gtype);
	}

	@Override
	public List<Map<String, String>> selectQuanJuansByArea(String cityid) {
		return apiJuanMapper.selectQuanJuansByArea(cityid);
	}

	@Override
	public boolean receiveJuan(String uid, String juanid, String filePath) {
		boolean ret_bool = false;
		try {
			// 生成用户的全城优惠劵二维码
			Map<String, Object> map = new HashMap<String, Object>();
			map = apiJuanMapper.selectById(juanid);
			String c_mod = map.get("c_mod").toString();
			if (c_mod.equals("1")) {
				String str_juan = juanid + "_" + uid + "_" + String.valueOf((new Date()).getTime() / 1000);
				String relative_file = "../webmvc_qrcodes/" + str_juan + ".jpg";
				// String file_name = filePath + relative_file;
				// File file = new File(file_name);
				// QRCodeUtils.qrCodeEncode(str_juan,file);
				// String qr_code = file_name;
				String qr_code = null;
				String enable = "0";
				// 领取
				ApiJuan juan = apiJuanMapper.selectJuanByCid(Integer.parseInt(juanid));
				Integer count = Integer.parseInt(juan.getMileages());
				Integer receive_count = juan.getReceive_count();
				if (receive_count < count) {
					ret_bool = apiJuanMapper.receiveQuanJuan(uid, juanid, enable, qr_code);
					// 更新统计
					ret_bool = apiJuanMapper.updateJuanReceiveCount(juanid);
				}

			} else { // 领取新人特权优惠劵
				ret_bool = apiJuanMapper.receiveJuan(uid, juanid);
			}
		} catch (Exception e) {
			// TODO
			e.printStackTrace();
		}
		//
		return ret_bool;
	}

	@Override
	public boolean useJuan(Integer juanid, String uid, String shopid) {
		boolean ret_bool = false;
		Integer isUsed = apiJuanTJMapper.juanIsUsed(uid, juanid);
		if (isUsed != null && isUsed == 0) {
			ret_bool = apiJuanMapper.updateJuanUsedCount(juanid.toString(), shopid);
			if (ret_bool) {
				apiJuanTJMapper.useJuan(juanid, uid);
			}
		}
		return ret_bool;
	}

	// 获取全城优惠券的列表
	@Override
	public List<Map<String, String>> getJuanListByUserid(String uid) {
		return apiJuanMapper.getJuanListByUserid(uid);
	}

	// 获取全城优惠券的列表
	@Override
	public List<Map<String, String>> getJuanByShopId(String shopid) {
		return apiJuanMapper.getJuanByShopId(shopid);
	}

	@Override
	public Map<String, Object> payJuan(String money, String[] citys, String[] shops, String uid,
			Map<String, Object> res, String products) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		String[] types = new String[] { "-1" };
		if (products != null && !"".equals(products)) {
			List<Integer> type = new ArrayList<Integer>();
			type = apiGoodsMapper.searTypeByGoodsId(products.split(","));// 参数是goodsid不是productsid
			if (type.size() > 0) {
				boolean boo = true;
				Integer one = type.get(0);
				for (int i = 0; i < type.size(); i++) {
					if (one != type.get(i)) {
						boo = false;
						break;
					}
				}
				if (boo) {
					Integer two = one;
					for (int i = 0; i < 3; i++) {
						ApiType at = apiTypeMapper.findTypeById(two);
						two = at.getcBelong();
						if (at.getcBelong() == 0 && at.getcGrade() == 0) {
							two = at.getcCode();
							break;
						}
					}
					types = new String[] { "-1", two + "" };
				}
			}
		}
		list = apiJuanMapper.payJuan(money, citys, shops, uid, types);
		res.put("datas", list);
		if (list.size() > 0) {
			res.put("error", "0000");
			res.put(Utils.MESSAGE, "success");
		} else {
			res.put("error", "1111");
			res.put(Utils.MESSAGE, "没有可用优惠券！");
		}
		return res;
	}

	@Override
	public boolean delete(String id) {
		return apiJuanMapper.delete(id);
	}

	@Override
	public Integer searIsEnable(String uid, String jid) {
		return apiJuanTJMapper.searIsEnable(uid, jid);
	}

	@Override
	public boolean updateJuan(ApiJuan apiJuan) {
		// TODO
		return apiJuanMapper.updateJuan(apiJuan);
	}

	@Override
	public List<Map<String, Object>> selectByUserId(String uid) {
		return apiJuanMapper.selectByUserId(uid);
	}

	@Override
	public ApiJuan selectJuanByCid(Integer cid) {
		return apiJuanMapper.selectJuanByCid(cid);
	}

	@Override
	public boolean updateCanReceive(String cid) {
		return apiJuanMapper.updateCanReceive(cid);
	}

	public List<ApiJuan> searchMemberJuanListPage(HashMap map, PageInfo page) {
		return apiJuanMapper.searchMemberJuanListPage(map, page);
	}

}
